package com.gamefx.mvc.classes
{
	import com.gamefx.mvc.Facade;
	
	import flash.system.Capabilities;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogLogger;


	public class Notifier
	{
		protected var facade:Facade = Facade.getInstance() as Facade;

		protected var logger:ILogger;
		protected var alcon:Class;

		
		
		public function onRegister():void
		{
			initializeLogger();
		}

		public function onRemove():void
		{
			destoryLogger();
		}
		
		
		/**
		 * 派发消息
		 * 
		 * @param notificationName
		 * @param body
		 * @param type
		 * 
		 */
		public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void 
		{
			facade.sendNotification( notificationName, body, type );
		}


		
		private function initializeLogger():void
		{
			logger = Log.getLogger(Facade.Name(this));

			try
			{
				alcon = getDefinitionByName("com.hexagonstar.util.debug.Debug") as Class;
			}
			catch (e:*)
			{
			}
			
			if (alcon)
			{
				logger.addEventListener(LogEvent.LOG, logger_logHandler);
			}
		}
		
		private function destoryLogger():void
		{
			logger.removeEventListener(LogEvent.LOG, logger_logHandler);
			logger = null;
		}

		private function logger_logHandler(event:LogEvent):void
		{
			var logger:LogLogger = event.currentTarget as LogLogger;
			var msg:String = "[" + LogEvent.getLevelString(event.level) + "] " + logger.category + " " + event.message;

			try
			{
				alcon["trace"](msg);
			}
			catch (e:*)
			{
			}
		}


	}
}
